package com.hulk.boot.web.exceptionhandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.base.Throwables;

import lombok.extern.slf4j.Slf4j;

/**
 * @author cmt
 * 
 */
@Slf4j
@ControllerAdvice
public class WebExceptionAdvice {

	
	
	  @ExceptionHandler(Exception.class) 
	  //@ResponseBody
	  public ModelAndView handleException(HttpServletRequest request, HttpServletResponse Response,  Exception  e) {  
	          
	       ModelAndView mav = new ModelAndView("exception");  
	       mav.addObject("code", e.getClass().getSimpleName());  
	       mav.addObject("message", Throwables.getStackTraceAsString(e));  
	       return mav;  
	   }  

	

	@ModelAttribute
	public void addAttribute(Model m) {
		m.addAttribute("msg", "bye");
		log.debug("============应用到所有@RequestMapping注解方法，在其执行之前把返回值放入Model");
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		log.debug("============应用到所有@RequestMapping注解方法，在其执行之前初始化数据绑定器");
	}

	

}